<template>
  <div style="width: 100%; height: 100%; background-image: url('/src/assets/banner/reginBj.jpg');background-size: cover; text-align: center; padding-top: 40px;">
    <el-form :model="loginForm" label-width="80px" class="login-form" :rules="rules" ref="loginForm2">
      <h3>登录</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="验证码" prop="capath">
        <el-input type="text" v-model="loginForm.captchaInput" autocomplete="off" />
        <el-image style="width: 120px; height: 38px" :src="imageBase64Data" @click="getCapath" />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="loginForm.role" placeholder="请选择角色">
          <el-option label="管理员" value="admin"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script setup>
import router from '@/router';
import { ElMessage } from 'element-plus';
import { ref, reactive } from 'vue';
import adminApi from "@/api/adminApi.js";
import {useTokenStore} from "@/stores/tokenStore.js";

const tokenStore = useTokenStore();
const loginForm = ref({
  username: '',
  password: '',
  captchaInput: '',
  key: '',
  role: ''
})
const imageBase64Data = ref('');
//表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名长度在 5 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  captchaInput: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ]
});
const loginForm2 = ref(null);
//定义登录方法
const login = () => {
  if (loginForm2.value) {
    loginForm2.value.validate((valid) => {
      if (valid) {
        if (loginForm.value.role == 'admin') {
          adminApi.login(loginForm.value)
            .then(resp => {
              if (resp.code == 10000) {
                //弹出成功消息
                ElMessage.success({
                  message: "登录成功",
                  duration: 1200,
                  onClose: () => {
                    //将jwt保存在store
                    tokenStore.updateToken(resp.data)
                    //保存jwt到seesionstorage
                    // sessionStorage.setItem('token', resp.data);
                    //跳转到后台界面
                    router.push('/admin/index');
                  }
                }
                )
              } else {
                //弹出失败消息
                ElMessage.error(resp.msg)
              }
            })
        }
      } else {

      };
    });
  }

}
//定于获取验证码的函数
const getCapath = () => {
  adminApi.captcha()
    .then(resp => {
      if (resp.code == 10000) {
        imageBase64Data.value = resp.data.imageBase64Data
        loginForm.value.key = resp.data.key
      } else {
        ElMessage.error(resp.data)
      }
    })
}
getCapath();
</script>

<style scoped>
.login-form {
  max-width: 400px;
  margin:0 auto;
  padding: 30px;
  border: 1px solid #eee;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>